$.strPad = function (i, l, s) {
    var o = i.toString();
    if (!s) {
        s = '0';
    }
    while (o.length < l) {
        o = s + o;
    }
    return o;
};

$(document).ready(function () {
    appBosch.date = new Date(appBosch.date);
    $('#updateTeams').click(function () {
        $.get(appBosch.updateMagicTeams, {}, function () {
            alert('Los equipos se han actualizado');
        });
    });
    $("#datepicker").kendoDatePicker({
        min: new Date('2015/01/01'),
        max: new Date(),
        value: appBosch.date,
        format: 'D',
        width: 300,
        change: function () {
            var dp = $("#datepicker").data('kendoDatePicker');
            var dt = dp.value();
            //var year = dt.getFullYear();
            //var month = $.strPad(dt.getMonth() + 1, 2);
            //var date = $.strPad(dt.getDate(), 2);
            //var dmy = [month, date, year].join('-');
            window.location.href = [appBosch.url, dt].join('/');
        }
    });
    var currentDate = appBosch.date;
    var year = currentDate.getFullYear();
    var month = $.strPad(currentDate.getMonth() + 1, 2);
    var date = $.strPad(currentDate.getDate(), 2);
    var ymd = [year, month, date].join('-');
    var urlRead = [appBosch.crud.read, ymd].join('/');
    var dataSource = new kendo.data.DataSource({
        data: appBosch.data
    });
    var widthCol = 180;
    $("#grid").kendoGrid({
        dataSource: dataSource,
        filterable: {
            mode: "row"
        },
        height: 500,
        reorderable: true,
        resizable: true,
        columnMenu: true,
        sortable: true,
        selectable: true,
        excel: {
            fileName: "rol-de-turnos.xlsx",
            filterable: true
        },
        pdf: {
            allPages: true,
            fileName: "rol-de-turnos.pdf"
        },
        columns: [
            {
                field: "nom",
                title: "Nomina",
                width: widthCol,
                locked: true
            },
            {
                title: headerDate(),
                columns: [
                    {
                        field: "l0",
                        title: "Línea",
                        width: widthCol,
                        values: appBosch.lines,
                        filterable: false
                    },
                    {
                        field: "s0",
                        title: "Turno",
                        width: widthCol,
                        values: appBosch.shifts,
                        filterable: false
                    },
                    {
                        field: "a0",
                        title: "Acción",
                        width: widthCol,
                        values: appBosch.assists,
                        filterable: false
                    }]
            },
            {
                title: headerDate(),
                columns: [
                    {
                        field: "l1",
                        title: "Línea",
                        width: widthCol,
                        values: appBosch.lines,
                        filterable: false
                    },
                    {
                        field: "s1",
                        title: "Turno",
                        width: widthCol,
                        values: appBosch.shifts,
                        filterable: false
                    },
                    {
                        field: "a1",
                        title: "Acción",
                        width: widthCol,
                        values: appBosch.assists,
                        filterable: false
                    }]
            },
            {
                title: headerDate(),
                columns: [
                    {
                        field: "l2",
                        title: "Línea",
                        width: widthCol,
                        values: appBosch.lines,
                        filterable: false
                    },
                    {
                        field: "s2",
                        title: "Turno",
                        width: widthCol,
                        values: appBosch.shifts,
                        filterable: false
                    },
                    {
                        field: "a2",
                        title: "Acción",
                        width: widthCol,
                        values: appBosch.assists,
                        filterable: false
                    }]
            },
            {
                title: headerDate(),
                columns: [
                    {
                        field: "l3",
                        title: "Línea",
                        width: widthCol,
                        values: appBosch.lines,
                        filterable: false
                    },
                    {
                        field: "s3",
                        title: "Turno",
                        width: widthCol,
                        values: appBosch.shifts,
                        filterable: false
                    },
                    {
                        field: "a3",
                        title: "Acción",
                        width: widthCol,
                        values: appBosch.assists,
                        filterable: false
                    }]
            },
            {
                title: headerDate(),
                columns: [
                    {
                        field: "l4",
                        title: "Línea",
                        width: widthCol,
                        values: appBosch.lines,
                        filterable: false
                    },
                    {
                        field: "s4",
                        title: "Turno",
                        width: widthCol,
                        values: appBosch.shifts,
                        filterable: false
                    },
                    {
                        field: "a4",
                        title: "Acción",
                        width: widthCol,
                        values: appBosch.assists,
                        filterable: false
                    }]
            },
            {
                title: headerDate(),
                columns: [
                    {
                        field: "l5",
                        title: "Línea",
                        width: widthCol,
                        values: appBosch.lines,
                        filterable: false
                    },
                    {
                        field: "s5",
                        title: "Turno",
                        width: widthCol,
                        values: appBosch.shifts,
                        filterable: false
                    },
                    {
                        field: "a5",
                        title: "Acción",
                        width: widthCol,
                        values: appBosch.assists,
                        filterable: false
                    }]
            },
            {
                title: headerDate(),
                columns: [
                    {
                        field: "l6",
                        title: "Línea",
                        width: widthCol,
                        values: appBosch.lines,
                        filterable: false
                    },
                    {
                        field: "s6",
                        title: "Turno",
                        width: widthCol,
                        values: appBosch.shifts,
                        filterable: false
                    },
                    {
                        field: "a6",
                        title: "Acción",
                        width: widthCol,
                        values: appBosch.assists,
                        filterable: false
                    }]
            }
        ],
        editable: false
    });
});

function headerDate() {
    var str = kendo.toString(appBosch.date, 'D');
    appBosch.date.setDate(appBosch.date.getDate() + 1);
    return str;
}